#!/usr/bin/env python

import sys

'''
S200029394L1C001R00100448392    2504552 2176518 2176567 1747    Cutibacterium acnes SZ2 DNA, complete genome
S200029394L1C001R00400282006    5779157 3775186 3775137 76760   Pseudomonas rhodesiae strain NL2019 chromosome, complete genome
S200029394L1C001R00400528588    740 192 241 99802   Spirometra erinaceieuropaei genome assembly S_erinaceieuropaei ,scaffold SPER_scaffold0234144
S200029394L1C001R00300196979    6261650 2312644 2312595 2605424 Pseudomonas sp. J380 chromosome, complete genome
S200029394L1C001R00300286599    801 440 391 99802   Spirometra erinaceieuropaei genome assembly S_erinaceieuropaei ,scaffold SPER_scaffold0229151
S200029394L1C001R00300461769    5743123 5553021 5553070 93222   Pandoraea sputorum strain NCTC13161 genome assembly, chromosome: 1
S200029394L1C001R00401125238    5779157 1426135 1426184 76760   Pseudomonas rhodesiae strain NL2019 chromosome, complete genome
S200029394L1C001R00401236338    2504552 1846784 1846735 1747    Cutibacterium acnes SZ2 DNA, complete genome
S200029394L1C001R00300647971    601 5   54  99802   Spirometra erinaceieuropaei genome assembly S_erinaceieuropaei ,scaffold SPER_scaffold0250835
S200029394L1C001R00200137152    548 297 346 99802   Spirometra erinaceieuropaei genome assembly S_erinaceieuropaei ,scaffold SPER_contig0205944
'''

a=set()

def readfa(fasta):
    fh = open(fasta,'r')
    seq = ''
    header = ''
    for line in fh:
        line = line.strip()
        if not line:
            continue
        if line[0] == '>':
            if seq :
                yield (header,seq)
                seq  = ''
            header = line[1:]
        else:
            seq = seq + line 
    if seq:
        yield (header,seq)

prefix=str(sys.argv[2]).split('.')[0]

for line in open(f'{sys.argv[2]}','r'):
    line=line.strip().split('\t')
    seqid=line[0]
    a.add(seqid)

f1=open(f'{prefix}_nt.fasta','w')

for i,j in readfa(sys.argv[1]):
    if i in a:
        f1.write(f">{i}\n{j}\n")

f1.close()
